﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Topics_Topics_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
          
            bd();
        }
    }

    private void bd()
    {
        CMSModel.CMSEntities cms = new CMSModel.CMSEntities();
        ddlVote.DataTextField = "Question";
        ddlVote.DataValueField = "TypeID";
        ddlVote.DataSource = cms.VoteType;
        ddlVote.DataBind();

        int tid = int.Parse(Request["tid"]);
        var content = cms.Topics2.First(x => x.Id == tid).Content;
        Topics2 t=null;
        if (!string.IsNullOrEmpty(content))
            t = Newtonsoft.Json.JsonConvert.DeserializeObject<Topics2>(content);
        else
            t = new Topics2();
        if (t.HDisplay != null)
        {
            try
            {
                CheckBox1.Checked = t.HDisplay[0];
                CheckBox2.Checked = t.HDisplay[1];
                CheckBox3.Checked = t.HDisplay[2];
                CheckBox4.Checked = t.HDisplay[3];
                CheckBox5.Checked = t.HDisplay[4];
                CheckBox6.Checked = t.HDisplay[5];
                CheckBox7.Checked = t.HDisplay[6];
            }
            catch
            {
            }
        }
        txtDaoyu.Text = t.DalYu;
        txtTable.Text = t.Table;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities cms = new CMSModel.CMSEntities();
        int tid = int.Parse(Request["tid"]);
        var  c= cms.Topics2.First(x => x.Id == tid);
        var content =c.Content;
        Topics2 t = null;
        if (!string.IsNullOrEmpty(content))
            t = Newtonsoft.Json.JsonConvert.DeserializeObject<Topics2>(content);
        else
            t = new Topics2();
        t.HDisplay = new List<bool>();
        try
        {

            t.HDisplay.Add ( CheckBox1.Checked);
            t.HDisplay.Add( CheckBox2.Checked);
            t.HDisplay.Add( CheckBox3.Checked);
            t.HDisplay.Add(  CheckBox4.Checked);
            t.HDisplay.Add(CheckBox5.Checked);
            t.HDisplay.Add( CheckBox6.Checked);
            t.HDisplay.Add( CheckBox7.Checked);
        }
        catch
        {
        }
        t.VoteTypeId = ddlVote.SelectedValue;
        t.DalYu= txtDaoyu.Text;
        t.Table = txtTable.Text;
        string str = Newtonsoft.Json.JsonConvert.SerializeObject(t);
        c.Content = str;
        cms.SaveChanges();
    }
}